package com.lynn.salary.web.action;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.lynn.salary.entity.User_;
import com.lynn.salary.entity.Role;
import com.lynn.salary.entity.SystemArgument;
import com.lynn.salary.entity.SystemFile;
import com.lynn.salary.pojo.FeedBack;
import com.lynn.salary.service.IUserService;
import com.lynn.salary.service.ISysArgumentService;
import com.lynn.salary.util.ObjectFactory;
import com.lynn.salary.util.ToolMethod;

public class UserManageAction extends BaseAction {

	private IUserService userService = (IUserService) ObjectFactory
			.getBean("userService");
	private ISysArgumentService sysArgumentService = (ISysArgumentService) ObjectFactory
			.getBean("sysArgumentService");
	private User_ employee;
	private List<Map<String, Object>> empList;
	private FeedBack feedBack;
	private String flag;
	private List<SystemArgument> posts;
	private List<SystemArgument> positions;
	private List<SystemArgument> nationalitys;
	private List<SystemArgument> departments;

	private File myfile;
	private String myfileContentType;
	private String myfileFileName;

	private String headImg;
	private String fileNumber;

	private String empId;
	private String empName;

	private List<Role> myRoles;
	private List<Role> allRoles;

	private List<String> newRoles;
	private List<String> notHave;

	@Override
	public String execute() throws Exception {
		totalCount = userService.totalCount();
		empList = userService.findEmployeeByLimit(pageNum, numPerPage);
		return SUCCESS;
	}

	public String save() throws Exception {
		String id = ToolMethod.toPinyin("" + employee.getName());
		if (id == null || id.trim().equals("")) {
			id = employee.getName();// 转拼音方法llynn
		}
		employee.setId(id);
		try {
			userService.save(employee);
		} catch (Exception e) {
			// TODO: handle exception
			id += ToolMethod.getCharAndNumr(2);
			userService.save(employee);
		}

		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		feedBack.setMessage("添加成功");
		feedBack.setRel("addEDialog");
		feedBack.setNavTabId("empp3");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("closeCurrent");
		return "saveSuccess";
	}

	public String delete() throws Exception {
		userService.delete(deleteId);
		feedBack = new FeedBack();
		feedBack.setStatusCode("200");
		feedBack.setMessage("删除成功");
		feedBack.setRel("delEDialog");
		feedBack.setNavTabId("empp3");
		feedBack.setForwardUrl("");
		feedBack.setCallbackType("");
		return "deleteSuccess";
	}

	public String hImg() throws Exception {

		if (myfile != null
				&& (fileNumber == null || fileNumber.trim().equals("") || fileNumber
						.equals("null"))) {
			String path = "d:/upload/imgs";
			File nf = new File(path);
			if (!nf.exists())
				nf.mkdirs();
			path += "/" + myfileFileName;
			nf = new File(path);
			ToolMethod.copy(myfile, nf);

			SystemFile sf = new SystemFile();
			sf.setAlterTime(new Date());
			sf.setCreator(loginUser.getLoginUser().getId());
			sf.setFileName(myfileFileName);
			sf.setTag(nf.getTotalSpace() + "");
			sf.setFileType(myfileContentType);
			sf.setNewTime(new Date());
			sf.setSavePath(path);
			sf.setWhoAlter(loginUser.getLoginUser().getId());

			try {
				sf = sysArgumentService.saveSf(sf);
			} catch (Exception e) {
				e.printStackTrace();
				throw e;
			}

			fileNumber = sf.getId();
			headImg = sf.getId();
			msg = "上传成功！点击保存按钮更新头像";
		} else if (fileNumber != null && !fileNumber.trim().equals("")
				&& !fileNumber.equals("null")) {
			loginUser.getLoginUser().setHeadImg(fileNumber);
			userService.updateEmployee(loginUser.getLoginUser());
			msg = "保存成功!";
		}

		return "headImg";
	}

	public String headImg() throws Exception {
		headImg = loginUser.getLoginUser().getHeadImg();
		return "headImg";
	}

	public String edit() throws Exception {

		posts = userService.findByType("职位");
		positions = userService.findByType("岗位");
		nationalitys = userService.findByType("民族");
		departments = userService.findByType("部门");
		// salarystandards=employeeService.findAllKinds();
		if (flag.equals("showEditPage")) {
			employee = userService.findEmployeeById(editId);
			return "showEditPage";
		}
		if (flag.equals("showEditSelf")) {
			String id = loginUser.getLoginUser().getId();
			// System.out.println(id);
			employee = loginUser.getLoginUser();
			return "showEditSelf";
		} else if (flag.equals("update")) {
			userService.updateEmployee(employee);
			feedBack = new FeedBack();
			feedBack.setStatusCode("200");
			feedBack.setMessage("更新成功");
			feedBack.setRel("updateEDialog");
			feedBack.setNavTabId("empp3");
			feedBack.setForwardUrl("");
			feedBack.setCallbackType("closeCurrent");
			return "editSuccess";
		} else if (flag.equals("updateSelf")) {
			userService.updateEmployee(employee);
			feedBack = new FeedBack();
			feedBack.setStatusCode("200");
			feedBack.setMessage("更新成功");
			feedBack.setRel("empp9");
			feedBack.setNavTabId("empp9");
			feedBack.setForwardUrl("");
			feedBack.setCallbackType("closeCurrent");
			return "editSuccess";

		}
		return "error";
	}

	public String empRoleAssign() throws Exception {

		if (newRoles != null) {

			System.out.println(newRoles);
			userService.updateEmpRoles(newRoles, empId);

			feedBack = new FeedBack();
			feedBack.setStatusCode("200");
			feedBack.setMessage("保存成功");

			return "feedback";

		} else if (empId != null && !empId.trim().equals("") && empName != null
				&& !empName.trim().equals("")
				&& !empId.toLowerCase().equals("null")) {

			myRoles = userService.getRoles(empId);
			allRoles = userService.findEmpNotContainRole(empId);

			empName = new String(empName.getBytes("ISO8859-1"), "UTF-8");

			return "empRoleAssignPage1";
		}

		return "empRoleAssign";
	}

	public User_ getEmployee() {
		return employee;
	}

	public void setEmployee(User_ employee) {
		this.employee = employee;
	}

	public List<Map<String, Object>> getEmpList() {
		return empList;
	}

	public void setEmpList(List<Map<String, Object>> empList) {
		this.empList = empList;
	}

	public FeedBack getFeedBack() {
		return feedBack;
	}

	public void setFeedBack(FeedBack feedBack) {
		this.feedBack = feedBack;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public List<SystemArgument> getPosts() {
		return posts;
	}

	public void setPosts(List<SystemArgument> posts) {
		this.posts = posts;
	}

	public List<SystemArgument> getPositions() {
		return positions;
	}

	public void setPositions(List<SystemArgument> positions) {
		this.positions = positions;
	}

	public List<SystemArgument> getNationalitys() {
		return nationalitys;
	}

	public void setNationalitys(List<SystemArgument> nationalitys) {
		this.nationalitys = nationalitys;
	}

	public List<SystemArgument> getDepartments() {
		return departments;
	}

	public void setDepartments(List<SystemArgument> departments) {
		this.departments = departments;
	}

	public IUserService getEmployeeService() {
		return userService;
	}

	public void setEmployeeService(IUserService employeeService) {
		this.userService = employeeService;
	}

	public String getHeadImg() {
		return headImg;
	}

	public ISysArgumentService getSysArgumentService() {
		return sysArgumentService;
	}

	public void setSysArgumentService(ISysArgumentService sysArgumentService) {
		this.sysArgumentService = sysArgumentService;
	}

	public void setHeadImg(String headImg) {
		this.headImg = headImg;
	}

	public String getFileNumber() {
		return fileNumber;
	}

	public void setFileNumber(String fileNumber) {
		this.fileNumber = fileNumber;
	}

	public File getMyfile() {
		return myfile;
	}

	public void setMyfile(File myfile) {
		this.myfile = myfile;
	}

	public String getMyfileContentType() {
		return myfileContentType;
	}

	public void setMyfileContentType(String myfileContentType) {
		this.myfileContentType = myfileContentType;
	}

	public String getMyfileFileName() {
		return myfileFileName;
	}

	public void setMyfileFileName(String myfileFileName) {
		this.myfileFileName = myfileFileName;
	}

	public String getEmpId() {
		return empId;
	}

	public void setEmpId(String empId) {
		this.empId = empId;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public List<Role> getMyRoles() {
		return myRoles;
	}

	public void setMyRoles(List<Role> myRoles) {
		this.myRoles = myRoles;
	}

	public List<Role> getAllRoles() {
		return allRoles;
	}

	public void setAllRoles(List<Role> allRoles) {
		this.allRoles = allRoles;
	}

	public List<String> getNewRoles() {
		return newRoles;
	}

	public void setNewRoles(List<String> newRoles) {
		this.newRoles = newRoles;
	}

	public List<String> getNotHave() {
		return notHave;
	}

	public void setNotHave(List<String> notHave) {
		this.notHave = notHave;
	}

	// public List<SalaryKind> getSalarystandards() {
	// return salarystandards;
	// }
	//
	// public void setSalarystandards(List<SalaryKind> salarystandards) {
	// this.salarystandards = salarystandards;
	// }
	//
	//	

}
